package com.reggie.console.controller;


import com.reggie.acl.dataobject.AddressBook;
import com.reggie.common.enums.ResponseCode;
import com.reggie.common.util.AssertUtils;
import com.reggie.common.util.ReggieContext;
import com.reggie.console.annotation.AuthRule;
import com.reggie.console.convertor.AddressBookDTOConvertor;
import com.reggie.console.dto.AddressBookDTO;
import com.reggie.console.dto.ResultDTO;
import com.reggie.console.enums.ReggieRole;
import com.reggie.console.util.ResultBuilder;
import com.reggie.service.model.request.AddressBookSaveRequest;
import com.reggie.service.model.request.AddressBookSetDefaultRequest;
import com.reggie.service.model.request.AddressBookUpdateRequest;
import com.reggie.service.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 地址管理 前端控制器
 * </p>
 *
 * @author yth
 * @since 2023-02-04
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
@AuthRule(roles = {ReggieRole.CUSTOMER})
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private AddressBookDTOConvertor addressBookDTOConvertor;

    /**
     * 根据登入用户id，查询所有地址
     *
     * @return 数据
     */
    @GetMapping("/list")
    public ResultDTO<List<AddressBookDTO>> list() {
        long userId = ReggieContext.getInstance().getUserId();
        List<AddressBook> addressBooks = addressBookService.listByUserId(userId);
        return ResultBuilder.ok(addressBooks.stream().map(addressBookDTOConvertor::toDTO)
                .collect(Collectors.toList()));
    }

    /**
     * 添加收货地址
     *
     * @param request 地址信息
     * @return 消息
     */
    @PostMapping
    public ResultDTO<String> save(@RequestBody AddressBookSaveRequest request) {
        long userId = ReggieContext.getInstance().getUserId();
        request.setUserId(userId);
        addressBookService.save(request);
        return ResultBuilder.ok("添加收货地址成功");
    }

    /**
     * 设置默认地址
     *
     * @param id 地址id
     * @return 消息
     */
    @PutMapping("/default/{id}")
    public ResultDTO<String> setDefault(@PathVariable long id) {
        long userId = ReggieContext.getInstance().getUserId();
        final AddressBookSetDefaultRequest request = new AddressBookSetDefaultRequest();
        request.setUserId(userId);
        request.setAddressBookId(id);
        addressBookService.setDefault(request);
        return ResultBuilder.ok("设置默认地址成功");
    }

    /**
     * 用户请求得到默认地址
     *
     * @return 数据
     */
    @GetMapping("/default")
    public ResultDTO<AddressBook> getDefault() {
        long userId = ReggieContext.getInstance().getUserId();
        AddressBook addressBook = addressBookService.getDefault(userId).orElse(null);
        return ResultBuilder.ok(addressBook);
    }

    /**
     * 根据id，查询数据
     *
     * @param id 地址簿id
     * @return 数据
     */
    @GetMapping("{id}")
    public ResultDTO<AddressBook> getById(@PathVariable long id) {
        AddressBook addressBook = addressBookService.getById(id).orElse(null);
        return ResultBuilder.ok(addressBook);
    }

    /**
     * 更新收货地址
     *
     * @param request 地址信息
     * @return 消息
     */
    @PutMapping
    public ResultDTO<String> update(@RequestBody AddressBookUpdateRequest request) {
        AssertUtils.nonNull(request.getId(), ResponseCode.CLIENT_ERROR);
        request.setUserId(ReggieContext.getInstance().getUserId());
        addressBookService.update(request);
        return ResultBuilder.ok("修改收货地址成功");
    }

    /**
     * 删除地址
     *
     * @param ids id集合
     * @return 消息
     */
    @DeleteMapping
    public ResultDTO<String> remove(@RequestParam List<Long> ids) {
        addressBookService.removeByIds(ids);
        return ResultBuilder.ok("删除地址成功");
    }
}

